const axios = require('axios')
const express = require("express")
const app = express()
const path = require('path')
const router = express.Router()




app.use(express.static("bao"));
app.use(express.static("view"));

// axios请求的一些配置参数
const instance = axios.create({
  // baseURL: 'http://api.w0824.com/api',
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 10000,
});



app.get("/index", function (req, res) {

  instance.get('http://127.0.0.1:5000/index')
    .then(function (response) {
      // 处理成功情况,response是接口返回的结果
      // 解构赋值,把response里的data数据拿出来
      var { data } = response;
      res.send(data);
      // res.end();
    })
    .catch(function (error) {
      // 处理错误情况,error返回错误的结果
      console.log(error);
    })

})


// 这个用不到但是要下载art-template
const artTemplate = require('art-template')
const expressTemplate = require('express-art-template')

//配置模板引擎
app.set('views',__dirname+'/view/')
app.engine('html',expressTemplate);
app.set('view engine','html')



app.get("/cha", function (req, res) {
  var { id } = req.query;

  instance.get(`http://127.0.0.1:5000/cha?id=${id}`)
    .then(function (response) {

      var { data } = response;
      console.log(data);
      res.render('cha.html',{data})

    })

    .catch(function (error) {

      console.log(error);
    })

})


app.get("/gai", function (req, res) {
  var { id } = req.query;

  instance.get(`http://127.0.0.1:5000/cha?id=${id}`)
    .then(function (response) {
      var { data } = response;
      res.render('gai.html',{data})

    })

    .catch(function (error) {

      console.log(error);
    })

})


app.get("/shan", function (req, res) {
  var { id } = req.query;

  instance.get(`http://127.0.0.1:5000/shan?id=${id}`)
    res.redirect('/')
})


app.get('/addTo', (req, res) => {
  res.sendFile(path.join(__dirname, './view/addTo.html'))
})
app.use('/',router)

app.listen(4000, () => {
  console.log('server is running ar port 4000');
})
